package com.draven.controller;

import com.draven.common.BusinessException;
import com.draven.common.Result;
import com.draven.service.SysUserService;
import com.draven.vo.LoginVO;
import com.draven.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/auth")
@Validated
public class AuthController {

    @Autowired
    private SysUserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Long> register(
            @NotBlank(message = "用户名不能为空") @Size(min = 3, max = 20, message = "用户名长度为3-20个字符") @RequestParam String username,
            @NotBlank(message = "密码不能为空") @Size(min = 6, max = 20, message = "密码长度为6-20个字符") @RequestParam String password,
            @NotBlank(message = "邮箱不能为空") @Email(message = "邮箱格式不正确") @RequestParam String email) {
        Long userId = userService.register(username, password, email);
        return Result.success(userId);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginVO> login(
            @NotBlank(message = "用户名不能为空") @RequestParam String username,
            @NotBlank(message = "密码不能为空") @RequestParam String password,
            @NotBlank(message = "验证码不能为空") @RequestParam String captcha,
            HttpSession session) {
        
        // 校验验证码
        String captchaCode = (String) session.getAttribute("captchaCode");
        if (captchaCode == null || !captchaCode.equalsIgnoreCase(captcha)) {
            throw new BusinessException(400, "验证码错误");
        }
        
        // 验证成功后，清除session中的验证码
        session.removeAttribute("captchaCode");
        
        // 执行登录
        LoginVO loginVO = userService.login(username, password);
        return Result.success(loginVO);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public Result<UserInfoVO> getUserInfo(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        UserInfoVO userInfo = userService.getUserInfo(userId);
        return Result.success(userInfo);
    }

    /**
     * 修改密码
     */
    @PostMapping("/updatePassword")
    public Result<Void> updatePassword(
            HttpServletRequest request,
            @NotBlank(message = "原密码不能为空") @RequestParam String oldPassword,
            @NotBlank(message = "新密码不能为空") @Size(min = 6, max = 20, message = "密码长度为6-20个字符") @RequestParam String newPassword) {
        Long userId = (Long) request.getAttribute("userId");
        userService.updatePassword(userId, oldPassword, newPassword);
        return Result.success();
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        // 前端清除token即可，后端无需处理
        return Result.success();
    }
} 